package top.infopub.mgr.mkt.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.domain.BidCurrencyDomain;
import top.infopub.mgr.mkt.jsonvo.AfterSummaryJsonVo;
import top.infopub.mgr.mkt.service.BidOpenResultService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.vo.BidCurrencyVo;
import top.infopub.model.mkt.AfterSummaryBid;
import top.infopub.model.mkt.AfterSummaryRank;
import top.infopub.model.mkt.BidOpenResult;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/4/27 15:56
 * @Version 1.0
 */
@Controller
@RequestMapping("/bidOpenResult")
public class BidOpenResultController extends GenericFlowController {

    @Autowired
    private BidOpenResultService bidOpenResultService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BidCurrencyDomain> queryDataByCondition(BidCurrencyVo bidCurrencyVo, ParamPage paramPage) throws ParseException {
        Page<BidCurrencyDomain> page = new Page<BidCurrencyDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = bidOpenResultService.selectPage(page,bidCurrencyVo);
        return RspVo.getSuccessResponse(page.getTotal(),page.getResult());
    }

     //保存
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr,Integer submitType){
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        AfterSummaryJsonVo aftSummJson = JSON.parseObject(str, AfterSummaryJsonVo.class);
        List<AfterSummaryRank> afterSummRankList = aftSummJson.getAfterSummRankList();
        //开标记录
        for (AfterSummaryRank afterSummaryRank : afterSummRankList) {
            afterSummaryRank.setAddBy(vo.getId());
            afterSummaryRank.setAddTime(new Date());
            afterSummaryRank.setAfterSummaryId(aftSummJson.getInfo().getId());
        }

        //更新中标单位报价
        List<AfterSummaryBid> afterSummBidList = aftSummJson.getAfterSummBidList();
        for (AfterSummaryBid afterSummaryBid : afterSummBidList) {
            afterSummaryBid.setAfterSummaryId(aftSummJson.getInfo().getId());
            afterSummaryBid.setAddBy(vo.getId());
            afterSummaryBid.setAddTime(new Date());
        }

        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(aftSummJson.getFileAtt()!=null){
            String[] fileAtt = aftSummJson.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, aftSummJson.getInfo().getProCode(), file);
                attachmentList.add(attachment);
            }
        }
        return bidOpenResultService.saveAll(vo,aftSummJson.getInfo(),afterSummRankList,afterSummBidList,attachmentList,submitType,bodyStr);
    }


    //详情
    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id, String proCode) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        BidOpenResult bidOpenResult = bidOpenResultService.selectById(id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        if (bidOpenResult != null) {
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidOpenResult.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), bidOpenResult.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(bidOpenResult.getProcessId()) && bidOpenResult.getProcessId().length() > 10) {
                request.setAttribute("processId", bidOpenResult.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(bidOpenResult.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidOpenResult.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        request.setAttribute("proCode",proCode);
        return "mkt/lay-bidOpeningResult-detail";
    }

    @RequestMapping("/queryDetailById")
    @ResponseBody
    public RspVo queryDetailById(String id) {
        AfterSummaryJsonVo afterSummJson = bidOpenResultService.queryDetailById(id);
        return RspVo.getSuccessResponse(afterSummJson);
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //开标结果审批
        proProcessManageService.auditProject(id, processId, status, auditMsg, null);
        return RspVo.getSuccessResponse();
    }

}
